import { http } from "@/utils/http";

interface ResponseDataForAlarmType {
  ALARM_TYPE: {
    COMMUNICATION: string;
    FAULT: string;
    LEAK: string;
    LOST_ELECTRICITY: string;
    OVER_LIMIT: string;
  };
}

export interface QueryForAlarmPointList {
  point: string;
  className: string;
  systemIdList: string[];
  alarmLevel: string;
  alarmType: string;
  pageNum: number;
  pageSize: number;
  countPointFlag: "" | "Y" | "N";
}

export interface PointItem {
  id: number;
  factoryId: string;
  factoryName: string;
  className: string;
  classNameDesc: string;
  point: string;
  alarmLevel: string;
  alarmType: string;
  alarmDesc: string;
  systemId: string;
  systemName: string;
  twinId: string;
  twinName: string;
  twinPosition: string;
  twinPositionDesc: string;
  countPointFlag: "" | "Y" | "N";
}

export type FormValueType = Pick<
  PointItem,
  | "id"
  | "factoryId"
  | "factoryName"
  | "className"
  | "classNameDesc"
  | "point"
  | "alarmLevel"
  | "alarmType"
  | "alarmDesc"
  | "systemId"
  | "systemName"
  | "twinId"
  | "twinName"
  | "twinPosition"
  | "twinPositionDesc"
  | "countPointFlag"
>;

export interface ResponseDataForPoints {
  pageNum: number;
  pageSize: number;
  pages: number;
  total: number;
  data: PointItem[];
}

export type ResponseDataForPoint = PointItem & {
  updateTime: string;
  createTime: string;
  includeInAnalysis: boolean;
};

export type AlarmPointRecordParams = Pick<QueryForAlarmPointList, "point" | "pageNum" | "pageSize">;

export type UpdateRecordItem = PointItem & { id: string; updateTime: string; updateUser: string };

export type UpdateRecordForAlarmPoint = Omit<ResponseDataForPoints, "data"> & {
  data: UpdateRecordItem[];
};

type AlarmDescriptionParams = {
  factoryId?: string;
  facCode?: string;
  id?: number;
  className?: string;
};

export interface TableItemType {
  updateUser: string;
  className: string;
  classNameDesc: string;
  updateTime: string;
  factoryName: string;
  content: string;
  id: number;
}

export type ResponseDataForAlarmDescription = TableItemType[];

type UpdateAlarmDescription = { id: number; content: string };

// 查询报警类型
export const getAlarmType = () => {
  return http.request<ResponseDataForAlarmType>("get", `/sharing/api/v1/alarm/point/enum/query`);
};
// 查询报警点位列表
export const getAlarmPointList = (data: QueryForAlarmPointList) => {
  return http.request<ResponseDataForPoints>("post", `/sharing/api/v1/alarm/point/query`, { data });
};
// 查询报警点位信息
export const getAlarmPointInfo = (data: { point: string }) => {
  return http.request<ResponseDataForPoint>("post", `/sharing/api/v1/alarm/point/get`, { data });
};
// 更新报警点位信息
export const updateAlarmPointInfo = (data: FormValueType) => {
  return http.request("post", `/sharing/api/v1/alarm/point/update`, { data });
};
// 查询报警点位更新记录
export const getAlarmUpdateRecord = (data: AlarmPointRecordParams) => {
  return http.request<UpdateRecordForAlarmPoint>("post", `/sharing/api/v1/alarm/point/record/query`, { data });
};
// 报警点位更新记录回退
export const pointRollback = (data: { id: string }) => {
  return http.request("post", `/sharing/api/v1/alarm/point/rollback`, { data });
};
// 查询报警说明记录
export const getAlarmDescriptionList = () => {
  return http.request<ResponseDataForAlarmDescription>("post", `/sharing/api/v1/alarm/point/describe/query`, { data: {} });
};
// 查询报警说明
export const getAlarmDescription = (data: AlarmDescriptionParams) => {
  return http.request<TableItemType>("post", `/sharing/api/v1/alarm/point/describe/get`, { data });
};
// 更新报警说明
export const updateAlarmDescription = (data: UpdateAlarmDescription) => {
  return http.request("post", `/sharing/api/v1/alarm/point/describe/update`, { data });
};
// 更新报警说明
export const importPoints = (data: FormData) => {
  return http.request("post", `/sharing/api/v1/alarm/point/import`, { data }, { headers: { "Content-type": "multipart/form-data" } });
};
